﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

using Shine.ManageUser;

namespace Shine.Framework
{
    public class ApplicationClass : IApplication, IApplicationEvents, IFramework, IDockableWindowManager, IMxApplication, IApplicationStatusEvents_Event
    {
        #region EventHandler
        public event OnDelayEventsChangedEventHandler OnDelayEvents;

        public event IApplicationStatusEvents_InitializedEventHandler Initialized;

        public event OnCurrentToolChangedEventHandler OnCurrentToolChangedEvent;

        public event OnDisplayStatusInfoEventHandler OnDisplayStatusInfoEvent;

        public event OnDockWindowAndRefreshEventHandler OnDockWindowAndRefreshEvent;

        public event OnDockWindowAtFixedLocationEventHandler OnDockWindowAtFixedLocationEvent;

        public event OnDockWindowEventHandler OnDockWindowEvent;

        public event OnMapPageSwitchEventHandler OnMapPageSwitchEvent;

        public event OnHideDockWindowEventHandler OnHideDockWindowEvent;

     
        #endregion

        #region User Define
        //private static LicenseInitializer m_AOLicenseInitializer = new LicenseInitializer();
      

        //private IPageLayoutControl2 m_PageLayoutControl = null;
        private SuperMap.UI.MapControl m_MapControl = null;
        private SuperMap.UI.SceneControl m_SceneControl = null;
        private SuperMap.UI.SceneControl m_SceneControl2 = null;
        private SuperMap.UI.LayersControl m_layerControl = null;
        private SuperMap.UI.WorkspaceControl m_wksControl = null;

        //private IToolbarControl m_ToolbarControl = null;
        private bool m_IsMapCtrlactive = true;
        private bool m_IsPageLayoutActive = false;
        private bool  m_IsSceneCtlActive=false;        private IBarManager m_BarManager;
        private IDockManager m_DockManager;
        private bool m_Delay;
        private IUser m_User;

       
        private string m_Caption;
        private bool m_PauseDrawing;
        public IMxDocument m_Document;
        private IStatusBar m_StatusBar;
        private bool m_Visible;
        private Form m_MainForm;
        private double m_SnapTolerance;
        private double m_Tolerance;

     

        #endregion

        #region Attribute
       
        public SuperMap.UI.MapControl MapControl
        {
            get
            {
                return this.m_MapControl;
            }
        }

        public SuperMap.UI.SceneControl SceneControl
        {
            get
            {
                return this.m_SceneControl;
            }
        }
        public SuperMap.UI.SceneControl SceneControl2
        {
            get
            {
                return this.m_SceneControl2;
            }
        }
        public SuperMap.UI.LayersControl LayersControl
        {
            get
            {
                return m_layerControl;
            }
         
        }

        public SuperMap.UI.WorkspaceControl WorkspaceControl
        {
            get
            {
                return m_wksControl;
            }
          
        }
       public SceneActionEnum SceneAction { get; set; }
        public bool IsMutualMap
        {
            get;
            set;

        }
       public bool IsScneCompare
        {
            get; set;
        }
        public string SelectedRoute
        {
            get;set;
        }

        public string PoiName
        {
            get;set;
        }
        public bool IsMapCtrlactive
        {
            get
            {
                return this.m_IsMapCtrlactive;
            }
        }
        public bool IsPageLayoutActive
        {
            get
            {
                return this.m_IsPageLayoutActive;
            }
        }
        public bool IsSceneCtlActive
        {
            get
            {
                return this.m_IsSceneCtlActive;
            }
        }
        public IUser User
        {
            set
            {
                m_User = value;
            }
            get
            {
                return m_User;
            }
        }

        public void RaiseMapPageSwitchEvent()
        {
            if(OnMapPageSwitchEvent!=null ) OnMapPageSwitchEvent();
        }
        //public System.Drawing.Point LocationPoint(int x, int y)
        //{
        //    int inthWnd = 0;
        //    if (IsMapCtrlactive)
        //        inthWnd = MapControl.hWnd;
        //    else
        //        inthWnd = PageLayoutControl.hWnd;
        //    System.IntPtr ptr = (System.IntPtr)inthWnd;
        //    Control c = Control.FromHandle(ptr);
        //    return c.PointToScreen(new System.Drawing.Point(x, y));
        //}

      

        public double SnapTolerance
        {
            get
            {
                return m_SnapTolerance;
            }
            set
            {
                m_SnapTolerance = value;
            }
        }

        public double Tolerance
        {
            get
            {
                return m_Tolerance;
            }
            set
            {
                m_Tolerance = value;
            }
        }

        public IBarManager BarManager
        {
            get
            {
                return m_BarManager;
            }
            set
            {
                m_BarManager = value;
            }
        }

        public IDockManager DockManager
        {
            get
            {
                return m_DockManager;
            }
            set
            {
                m_DockManager = value;
            }
        }

        private ICommandItem m_CurrentTool;

        public ICommandItem CurrentTool
        {
            get
            {
                return m_CurrentTool;
            }
            set
            {
                //m_CurrentTool = value;
                //if (m_IsMapCtrlactive)
                //{
                //    if (this.m_MapControl != null && m_CurrentTool != null)
                //    {
                //        this.m_MapControl.CurrentTool = m_CurrentTool.Command as ITool;
                //    }
                //}
                //else if (m_IsPageLayoutActive )
                //{
                //    if (this.m_PageLayoutControl != null && m_CurrentTool != null)
                //    {
                //        this.m_PageLayoutControl.CurrentTool = m_CurrentTool.Command as ITool;
                //    }
                //}
                //else
                //{
                //    if (this.m_SceneControl !=null && m_CurrentTool != null)
                //    {
                //        this.m_SceneControl.CurrentTool = m_CurrentTool.Command as ITool;
                //    }
                //}
                //if (this.OnCurrentToolChangedEvent != null)
                //{
                //    this.OnCurrentToolChangedEvent(m_CurrentTool.Command as ITool);
                //}
            }
        }

        public ITool Tool
        {
            get
            {
                //if (m_IsMapCtrlactive)
                //{
                //    if (this.m_MapControl != null)
                //        return this.m_MapControl.CurrentTool;
                //    else
                //        return null;
                //}
                //else if (m_IsPageLayoutActive)
                //{
                //    if (this.m_PageLayoutControl != null)
                //        return this.m_PageLayoutControl.CurrentTool;
                //    else
                //        return null;
                //}
                //else
                //{
                //    if (this.m_SceneControl != null)
                //        return this.m_SceneControl.CurrentTool;
                //    else
                        return null;
               // }
            }
            set
            {
                //if (m_IsMapCtrlactive)
                //{
                //    if (this.m_MapControl != null)
                //    {
                //        this.m_MapControl.CurrentTool = value;
                //    }
                //}
                //else if (m_IsPageLayoutActive)
                //{
                //    if (this.m_PageLayoutControl != null)
                //    {
                //        this.m_PageLayoutControl.CurrentTool = value;
                //    }
                //}
                //else
                //{
                //    if (this.m_SceneControl != null)
                //    {
                //        this.m_SceneControl.CurrentTool = value;
                //    }
                //}
                //if (this.OnCurrentToolChangedEvent != null)
                //{
                //    this.OnCurrentToolChangedEvent(value);
                //}
            }
        }

        //public object Object
        //{
        //    get
        //    {
        //        if (m_IsMapCtrlactive)
        //            return m_MapControl.Object;
        //        else if (m_IsPageLayoutActive)
        //            return m_PageLayoutControl.Object;
        //        else
        //            return m_SceneControl.Object;
        //    }
        //    set
        //    {
        //        if (value == null)
        //        {
        //            this.m_MapControl = null;
        //            this.m_PageLayoutControl = null;
        //            this.m_SceneControl = null;
        //        }
        //        else if (value is IMapControl3)
        //        {
        //            this.m_MapControl = (IMapControl3)value;
        //        }
        //        else if (value is IPageLayoutControl2)
        //        {
        //            this.m_PageLayoutControl = (IPageLayoutControl2)value;
        //        }
        //        else if (value is ISceneControl)
        //        {
        //            this.m_SceneControl = (ISceneControl)value;
        //        }
        //    }
        //}

        public Form MdiForm
        {
            get
            {
                return this.m_MainForm;
            }
            set
            {
                this.m_MainForm = value;
            }
        }

      
        public bool Visible
        {
            get
            {
                return m_Visible;
            }
            set
            {
                m_Visible = value;
            }
        }

        public IStatusBar StatusBar
        {
            get
            {
                return m_StatusBar;
            }
        }

     
        public int hWnd
        {
            get
            {
                if (this.m_MainForm != null)
                    return this.m_MainForm.Handle.ToInt32();
                return 0;
            }
        }

        public string Name
        {
            get
            {
                return "三维地图展示系统";//这里可以设置系统的名称，通过配置文件读取
            }
        }

        public IDocument Document
        {
            get
            {
                
                try
                {
                   // m_Document.ActiveView = this.MapControl.ActiveView;
                   // m_Document.PageLayout = this.PageLayout;
                    return m_Document as IDocument;
                }
                catch
                {
                    return null;
                }
            }
        }

        //public ISet DataWindows
        //{
        //    get
        //    {
        //        return m_DataWindows;
        //    }
        //}

     

        public bool PauseDrawing
        {
            get
            {
                return m_PauseDrawing;
            }
            set
            {
                m_PauseDrawing = value;
            }
        }

        public string Caption
        {
            get
            {
                return m_Caption;
            }
            set
            {
                m_Caption = value;
            }
        }

        //public IAppDisplay Display
        //{
        //    get
        //    {
        //        return this.ActiveView.ScreenDisplay as IAppDisplay;
        //    }
        //}

        //public IPaper Paper
        //{
        //    get
        //    {
        //        return m_PageLayoutControl.Printer.Paper;
        //    }
        //}

        //public IPrinter Printer
        //{
        //    get
        //    {
        //        return this.m_PageLayoutControl.Printer;
        //    }
        //    set
        //    {
        //        this.m_PageLayoutControl.Printer = value;
        //    }
        //}

        //public IActiveView ActiveView
        //{
        //    get
        //    {
        //        return this.FocusMap as IActiveView;
        //        //if (this.m_MapControl != null)
        //        //{
        //        //    return this.m_MapControl.ActiveView;
        //        //}
        //        //if (this.m_PageLayoutControl != null)
        //        //{
        //        //    return this.m_PageLayoutControl.ActiveView;
        //        //}
        //        //return null;
        //    }
        //}

        //public IMap FocusMap
        //{
        //    get
        //    {
        //        if (this.m_MapControl != null)
        //        {
        //            return this.m_MapControl.Map;
        //        }
        //        if (this.m_PageLayoutControl != null)
        //        {
        //            return this.m_PageLayoutControl.ActiveView.FocusMap;
        //        }
        //        return null;
        //    }
        //}

        //public IOperationStack OperationStack
        //{
        //    get
        //    {
        //        return this.m_Document.OperationStack;
        //    }
        //}

        //public IPageLayout PageLayout
        //{
        //    get
        //    {
        //        return this.m_PageLayoutControl.PageLayout;
        //    }
        //}

        //public ISelectionEnvironment SelectionEnvironment
        //{
        //    get
        //    {
        //        return this.m_SelectionEnvironment;
        //    }
        //    set
        //    {
        //        this.m_SelectionEnvironment = value;
        //    }
        //}

        //public IStyleGallery StyleGallery
        //{
        //    get
        //    {
        //        return this.m_StyleGallery;
        //    }
        //    set
        //    {
        //        this.m_StyleGallery = value;
        //    }
        //}
        #endregion

        /// <summary>
        /// ApplicationClass
        /// </summary>
        /// 
        public ApplicationClass()
        {
           

           // m_Document = new MxDocumentClass();
         //   (m_Document as IDocument).Parent = this;
            m_StatusBar = new StatusBarClass();
            m_DockManager = new DockManagerClass();
            m_BarManager = new BarManagerClass();
           
        }

     

        public void DelayEvents(bool delay)
        {
            this.m_Delay = delay;
        }

        public void ExecuteCommand(ICommand pCommand)
        {
            try
            {
                //this.m_StatusBar.set_Message(0, "当前操作：" + pCommand.Tooltip);
                pCommand.OnCreate(this);
                pCommand.OnClick();
            }
            catch (Exception ex)
            {
//                MessageBox.Show(ex.Message);
                //ZJGIS.ADF.BaseClasses.ControlUtility.ShowErrorMsgbox("程序错误,错误信息:"+ex.Message);
            }
        }

        public void ResetCurrentTool()
        {
            //if (this.m_MapControl != null)
            //{
            //    this.m_MapControl.CurrentTool = null;
            //}
            //else if (this.m_PageLayoutControl != null)
            //{
            //    this.m_PageLayoutControl.CurrentTool = null;
            //}
        }

        private void SetBuddies(object buddy)
        {
            try
            {
                //if (buddy == null)
                //    throw new Exception("SetBuddies:\r\nTarget Buddy Control is not initialized!");
                //if (m_TOCControl != null)
                //{
                //    m_TOCControl.SetBuddyControl(buddy);
                //}
                //if (m_ToolbarControl != null)
                //{
                //    m_ToolbarControl.SetBuddyControl(buddy);
                //}
            }
            catch (Exception ex )
            {
                throw new Exception(string.Format("SetBuddies:\r\n{0}", ex.Message));
            }
        }

        public void BindControls(SuperMap.UI.MapControl mapControl,SuperMap.UI.SceneControl sceneControl, SuperMap.UI.SceneControl sceneControl2,SuperMap.UI.WorkspaceControl wksControl,SuperMap.UI.LayersControl lyrControl, bool activateMapFirst)
        {
            
            this.m_MapControl = mapControl;
            this.m_SceneControl = sceneControl;
            this.m_SceneControl2 = sceneControl2;
            this.m_wksControl = wksControl;
            this.m_layerControl = lyrControl;
           // this.BindControl(activateMapFirst);
        }

        //private void BindControl(bool activateMapFirst)
        //{
        //    if (m_PageLayoutControl == null || m_MapControl == null)
        //        throw new Exception("MapControl or PageLayoutControl are not initialized!");
        //    IMap newMap = new MapClass();
        //    newMap.Name = "Map";
        //    IMaps maps = new Maps();
        //    maps.Add(newMap);
        //    m_PageLayoutControl.PageLayout.ReplaceMaps(maps);
        //    m_MapControl.Map = newMap;
        //    if (activateMapFirst)
        //        ActivateMapControl();
        //    else
        //        ActivatePageLayoutControl();
        //}

        public void ActivateMapControl()
        {
            //if (this.m_PageLayoutControl != null)
            //{
            //    m_PageLayoutControl.ActiveView.Deactivate();
            //}
            //m_MapControl.ActiveView.Activate(m_MapControl.hWnd);
            //(m_MapControl.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGeography | esriViewDrawPhase.esriViewGeoSelection | esriViewDrawPhase.esriViewGraphics, null, (m_MapControl.Map as IActiveView).Extent);
            //m_IsMapCtrlactive = true;
            //m_IsPageLayoutActive = false;
            //m_IsSceneCtlActive = false;

            //SetBuddies(this.m_MapControl.Object);
        }

        public void ActivatePageLayoutControl()
        {
            //IMaps maps = new Maps();
            //maps.Add(this.m_MapControl.Map);
            //m_MapControl.ActiveView.Deactivate();
            //this.m_PageLayoutControl.PageLayout.ReplaceMaps(maps);
            //m_PageLayoutControl.ActiveView.Activate(m_PageLayoutControl.hWnd);
            //m_IsMapCtrlactive = false;
            //m_IsPageLayoutActive = true ;
            //m_IsSceneCtlActive = false;
            //SetBuddies(this.m_PageLayoutControl.Object);
        }
        public void ActivateSceneControl()
        {
            //m_IsMapCtrlactive = false;
            //m_IsPageLayoutActive = false ;
            //m_IsSceneCtlActive = true;
            //SetBuddies(this.m_SceneControl.Object);
        }
        public object GetControlUserData(string strName)
        {
            Control m_Control = this.DockManager.GetControlFromDockPanel(strName);
            return FindDockableWindowData(m_Control);
        }

        private object FindDockableWindowData(Control m_Control)
        {
            if (m_Control is IDockableWindow)
                return (m_Control as IDockableWindow).UserData;
            else
            {
                foreach (Control c1 in m_Control.Controls)
                {
                    return FindDockableWindowData(c1);
                }
            }
            return null;
        }

        public void DisplayStatusInfo(string sInfo)
        {
            m_StatusBar.set_Message(0, sInfo);
        }

        #region Window
        public void ClosingWindow(object pWindow)
        {

        }

        public void DockWindow(object pWindow)
        {

        }

        public void DockWindow(object pWindow, System.Drawing.Point pt)
        {

        }

        public void DockWindowAndRefresh(object pWindow)
        {

        }

        //public IDockableWindow GetDockableWindow(UID dockWnd)
        //{
        //    Guid m_Guid = new Guid(dockWnd.Value.ToString());
        //    return Activator.CreateInstance(Type.GetTypeFromCLSID(m_Guid)) as IDockableWindow;
        //}

        public void HideWindow(object pWindow)
        {
            Form m_Form = pWindow as Form;
            m_Form.Hide();
        }
        #endregion

        public void LostFocusToClose(object pWindow)
        {
            (pWindow as Form).Close();
        }

        public void UpdateContent()
        {
            MessageBox.Show("这块需要进行完善！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }

        public void CopyToClipboard()
        {
            MessageBox.Show("这块需要进行完善！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }

        public void Export()
        {
            MessageBox.Show("这块需要进行完善！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;

        }

        public string Next()
        {
            MessageBox.Show("这块需要进行完善！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            // return;
            return null;
        }

        public void Reset()
        {
            MessageBox.Show("这块需要进行完善！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }

        #region Document
        public void PrintDocument()
        {
            MessageBox.Show("这块需要进行完善！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
          
           // m_PageLayoutControl.PrintPageLayout(1, 1, 0);
        }

        public void PrintPreview()
        {
            MessageBox.Show("这块需要进行完善！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }

        //public void ReplaceMap(IMap newMap)
        //{
        //    //if (newMap == null)
        //    //    throw new Exception("ReplaceMap:\r\nNew map for replacement is not initialized!");
        //    //if (this.m_PageLayoutControl == null || this.m_MapControl == null)
        //    //    throw new Exception("ReplaceMap:\r\nEither MapControl or PageLayoutControl are not initialized!");
        //    //IMaps maps = new Maps();
        //    //maps.Add(newMap);
        //    //bool bIsMapActive = m_IsMapCtrlactive;
        //    //this.ActivatePageLayoutControl();
        //    //this.m_PageLayoutControl.PageLayout.ReplaceMaps(maps);

        //    //m_MapControl.Map = newMap;

        //    //if (bIsMapActive)
        //    //{
        //    //    this.ActivateMapControl();
           
        //    //   // (m_MapControl.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGeography | esriViewDrawPhase.esriViewGeoSelection | esriViewDrawPhase.esriViewGraphics, null, (m_MapControl.Map as IActiveView).Extent);
        //    //}
        //    //else
        //    //{
        //    //    this.ActivatePageLayoutControl();
              
        //    //   // m_PageLayoutControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography | esriViewDrawPhase.esriViewGeoSelection | esriViewDrawPhase.esriViewGraphics, null, m_PageLayoutControl.ActiveView.Extent);
        //    //}
        //}

        public void SaveAsDocument(string saveAsPath)
        {
            (m_Document as IDocument).SaveAsDocument(saveAsPath);
            OpenDocument(saveAsPath);
           // ZJGIS.ADF.BaseClasses.ControlUtility.ShowInfomationMsgbox("保存成功!");
        }

        public void OpenDocument(string path)
        {
            //try
            //{
            //    if (m_IsSceneCtlActive == false)
            //    {
            //        IEngineEditor m_engineEditor = new EngineEditorClass();
            //        if (m_engineEditor.EditState == esriEngineEditState.esriEngineStateEditing)
            //        {
            //            if (m_engineEditor.HasEdits())
            //            {
            //                DialogResult dr = ZJGIS.ADF.BaseClasses.ControlUtility.ShowYesOrNoQueryMsgBox("是否保存编辑？");
            //                if (dr == DialogResult.Yes)
            //                    m_engineEditor.StopEditing(true);
            //                else if (dr == DialogResult.No)
            //                    m_engineEditor.StopEditing(false);

            //                System.GC.Collect();
            //            }
            //            else
            //            {
            //                m_engineEditor.StopEditing(false);
            //            }
            //        }
            //    }

            //    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;

            //    if (m_IsMapCtrlactive || m_IsPageLayoutActive)
            //    {
            //        string strExt = System.IO.Path.GetExtension(path).ToLower();
            //        if (strExt.Equals(".mxd") || strExt.Equals(".mxt") || strExt.Equals(".pmf"))
            //        {
            //            if (m_MapControl != null && m_MapControl.CheckMxFile(path))
            //            {

            //                (m_Document as IDocument).Open(path, "");
            //                IMap map = (m_Document as IDocument).get_Map(0);
            //                map.Name = "Layers";
            //                ReplaceMap(map);

            //                string file = System.IO.Path.GetFileName(path);
            //                string caption = (this as IFramework).MdiForm.Text;
            //                if (caption.IndexOf("[") > 0)
            //                {
            //                    caption = caption.Substring(0, caption.IndexOf("[")) + "[" + file + "]";
            //                }
            //                else
            //                {
            //                    caption = caption + "[" + file + "]";
            //                }
            //                (this as IFramework).MdiForm.Text = caption;
            //            }
            //            m_Document.ActiveView = this.ActiveView;
            //            m_Document.PageLayout = this.PageLayout;

            //            ESRI.ArcGIS.Carto.IActiveViewEvents_Event actviewEvents = m_Document.ActiveView as ESRI.ArcGIS.Carto.IActiveViewEvents_Event;
            //            actviewEvents.ContentsChanged -= new ESRI.ArcGIS.Carto.IActiveViewEvents_ContentsChangedEventHandler(actviewEvents_ContentsChanged);
            //            actviewEvents.ContentsChanged += new ESRI.ArcGIS.Carto.IActiveViewEvents_ContentsChangedEventHandler(actviewEvents_ContentsChanged);
            //        }
            //        m_Document.ActiveView.ScreenDisplay.UpdateWindow();
            //    }
            //    else if (m_IsSceneCtlActive)
            //    {
            //        m_SceneControl.LoadSxFile(path);
            //    }

            //    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message);
            //}
        }

        void actviewEvents_ContentsChanged()
        {
         
        }

        public void SaveDocument(string saveAsPath)
        {
            (m_Document as IDocument).SaveDocument(saveAsPath);         
           // ZJGIS.ADF.BaseClasses.ControlUtility.ShowInfomationMsgbox("保存成功!");
        }

        public void NewDocument(bool selectTemplate, string templatePath)
        {
            //try
            //{
            //    IEngineEditor m_engineEditor = new EngineEditorClass();
            //    if (m_engineEditor.EditState == esriEngineEditState.esriEngineStateEditing)
            //    {
            //        if (m_engineEditor.HasEdits())
            //        {
            //            DialogResult dr =ZJGIS.ADF.BaseClasses.ControlUtility.ShowYesOrNoQueryMsgBox("是否保存编辑？");
            //            if (dr == DialogResult.Yes)
            //                m_engineEditor.StopEditing(true);
            //            else if (dr == DialogResult.No)
            //                m_engineEditor.StopEditing(false);

            //            System.GC.Collect();
            //        }
            //        else
            //        {
            //            m_engineEditor.StopEditing(false);
            //        }

            //    }


            //    this.m_MapControl.Map.ClearLayers();
            //    this.m_MapControl.Map.SpatialReference = null;
            //    (m_Document as MxDocumentClass).m_MapDocument.Close();

               
            //    (m_Document as MxDocumentClass).New("无标题.mxd");

            //    string caption = (this as IFramework).MdiForm.Text;
            //    if (caption.IndexOf("[") > 0)
            //    {
            //        caption = caption.Substring(0, caption.IndexOf("[")) + "[无标题.mxd]"; 
            //    }
            //    else
            //    {
            //        caption = caption + "[无标题.mxd]";
            //    }
            //    (this as IFramework).MdiForm.Text = caption;

            //    IMap map = (m_Document as IDocument).get_Map(0);
            //    map.Name = "Layers";
            //    ReplaceMap(map);

            //    m_Document.ActiveView = this.ActiveView;
            //    m_Document.PageLayout = this.PageLayout;

              

            //    ESRI.ArcGIS.Carto.IActiveViewEvents_Event actviewEvents = m_Document.ActiveView as ESRI.ArcGIS.Carto.IActiveViewEvents_Event;
            //    actviewEvents.ContentsChanged -= new ESRI.ArcGIS.Carto.IActiveViewEvents_ContentsChangedEventHandler(actviewEvents_ContentsChanged);
            //    actviewEvents.ContentsChanged += new ESRI.ArcGIS.Carto.IActiveViewEvents_ContentsChangedEventHandler(actviewEvents_ContentsChanged);

            //    //if (selectTemplate)
            //    //{
            //    //    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            //    //    if (templatePath.EndsWith(".mxt"))
            //    //    {
            //    //        if (m_MapControl != null && m_MapControl.CheckMxFile(templatePath))
            //    //        {
            //    //            (m_Document as IDocument).Open(templatePath, "");
            //    //            IMap map = (m_Document as IDocument).get_Map(0);
            //    //            ReplaceMap(map);
            //    //        }
            //    //    }
            //    //    System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
            //    //}
            //}
            //catch (Exception ex)
            //{
            //    //MessageBox.Show(ex.Message);
            //}
        }
        #endregion

        public void Shutdown()
        {
            (m_Document as IDocument).Close();
            //m_AOLicenseInitializer.ShutdownApplication();
        }

        public bool IsDialogVisible(int dialogID)
        {
            return false;
        }

        public void RefreshWindow()
        {
            m_MainForm.Refresh();
        }

        public object ShowDialog(int dialogID, ref object bShow)
        {
            return null;
        }

        #region 资源树右键菜单
        //public void RemoveLayersFromMapControl(List<IDataset> datasetsall)
        //{
        //    if (this.m_MapControl == null)
        //        return;
        //    foreach (IDataset dataset in datasetsall)
        //    {
        //        if (dataset.Type == esriDatasetType.esriDTFeatureDataset)
        //        {
        //            IEnumDataset datasets = dataset.Subsets;
        //            IDataset t_Dataset = datasets.Next();
        //            while (t_Dataset != null)
        //            {
        //                IFeatureClass fClass = t_Dataset as IFeatureClass;
        //                foreach (IFeatureLayer layer in FindLayersByDs(this.m_MapControl.Map, (IDataset)fClass))
        //                {
        //                    this.m_MapControl.Map.DeleteLayer(layer);
        //                }
        //                t_Dataset = datasets.Next();
        //            }
        //        }
        //        else
        //        {
        //            //ym 20091203 update
        //            List<ILayer> m_Layers = FindLayersByDs(this.m_MapControl.Map, dataset);
        //            foreach (ILayer layer in m_Layers)
        //            {
        //                this.m_MapControl.Map.DeleteLayer(layer);
        //            }
        //        }
        //    }
        //}

        //public void VisibleLayersFromMapControl(List<IDataset> datasetsall)
        //{
        //    if (this.m_MapControl == null)
        //        return;
        //    foreach (IDataset dataset in datasetsall)
        //    {
        //        if (dataset.Type == esriDatasetType.esriDTFeatureDataset)
        //        {
        //            IEnumDataset datasets = dataset.Subsets;
        //            IDataset t_Dataset = datasets.Next();
        //            while (t_Dataset != null)
        //            {
        //                IFeatureClass fClass = t_Dataset as IFeatureClass;
        //                foreach (IFeatureLayer layer in FindLayersByDs(this.m_MapControl.Map, (IDataset)fClass))
        //                {
        //                    layer.Visible = true;
        //                }
        //                t_Dataset = datasets.Next();
        //            }
        //        }
        //        else
        //        {
        //            //ym 20091203 update
        //            List<ILayer> m_Layers = FindLayersByDs(this.m_MapControl.Map, dataset);
        //            foreach (ILayer layer in m_Layers)
        //            {
        //                layer.Visible = true;
        //            }
        //        }
        //    }
        //    //this.m_MapControl.ActiveView.Refresh();
        //    this.m_MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography | esriViewDrawPhase.esriViewGeoSelection | esriViewDrawPhase.esriViewGraphics, null, this.m_MapControl.ActiveView.Extent);
        //}

        //public void UnVisibleLayersFromMapControl(List<IDataset> datasetsall)
        //{
        //    if (this.m_MapControl == null) return;
        //    foreach (IDataset dataset in datasetsall)
        //    {
        //        if (dataset.Type == esriDatasetType.esriDTFeatureDataset)
        //        {
        //            IEnumDataset datasets = dataset.Subsets;
        //            IDataset t_Dataset = datasets.Next();
        //            while (t_Dataset != null)
        //            {
        //                IFeatureClass fClass = t_Dataset as IFeatureClass;
        //                foreach (IFeatureLayer layer in FindLayersByDs(this.m_MapControl.Map, (IDataset)fClass))
        //                {
        //                    layer.Visible = false;
        //                }
        //                t_Dataset = datasets.Next();
        //            }
        //        }
        //        else
        //        {
        //            //ym 20091203 update
        //            List<ILayer> m_Layers = FindLayersByDs(this.m_MapControl.Map, dataset);
        //            foreach (ILayer layer in m_Layers)
        //            {
        //                layer.Visible = false;
        //            }
        //        }
        //    }
        //    //this.m_MapControl.ActiveView.Refresh();
        //    this.m_MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography | esriViewDrawPhase.esriViewGeoSelection | esriViewDrawPhase.esriViewGraphics, null, this.m_MapControl.ActiveView.Extent);

        //}

        //public static List<ILayer> FindLayersByDs(IMap pMap, IDataset ds)
        //{
        //    List<ILayer> pLayers = new List<ILayer>();

        //    ArrayList arrayList = GetAllMapFeatureLayer(pMap);
        //    foreach (object o in arrayList)
        //    {
        //        ILayer pLayer = o as ILayer;
        //        IDataset pTempDs = null;
        //        if (pLayer is IFeatureLayer)
        //        {
        //            pTempDs = ((IFeatureLayer)pLayer).FeatureClass as IDataset;
        //            if (ds.Workspace.Equals(pTempDs.Workspace))
        //            {
        //                //sdc后缀的要素类文件objectclassid总为-1
        //                if (((IFeatureClass)pTempDs).ObjectClassID == -1 || ((IFeatureClass)ds).ObjectClassID == -1)
        //                {
        //                    if (ds.Name == pTempDs.Name)
        //                    {
        //                        pLayers.Add(pLayer);
        //                    }
        //                }
        //                else
        //                {
        //                    if (((IFeatureClass)pTempDs).ObjectClassID == ((IFeatureClass)ds).ObjectClassID)
        //                    {
        //                        pLayers.Add(pLayer);
        //                    }
        //                }
        //            }
        //        }
        //        else if (pLayer is IRasterLayer)
        //        {
        //            if (pLayer.Name == ds.Name)
        //            {
        //                pLayers.Add(pLayer);
        //            }
        //        }
        //        else if (pLayer is IGdbRasterCatalogLayer)
        //        {
        //            if (pLayer.Name == ds.Name)
        //            {
        //                pLayers.Add(pLayer);
        //            }
        //        }
        //    }
        //    return pLayers;
        //}

        //public static ArrayList GetAllMapFeatureLayer(IMap pMap)
        //{
        //    ArrayList allMapLayer = new ArrayList();
        //    ILayer pLayerTemp;

        //    if (pMap == null)
        //    {
        //        return allMapLayer;
        //    }

        //    for (int i = 0; i < pMap.LayerCount; i++)
        //    {
        //        pLayerTemp = pMap.get_Layer(i);
        //        GetSubLayer(pLayerTemp, ref allMapLayer);
        //    }

        //    return allMapLayer;
        //}

        //private static void GetSubLayer(ILayer pLayer, ref ArrayList allMapLayer)
        //{
        //    ICompositeLayer pCompositeLayer;
        //    if (pLayer is IGroupLayer)
        //    {
        //        pCompositeLayer = pLayer as ICompositeLayer;
        //        for (int i = 0; i < pCompositeLayer.Count; i++)
        //        {
        //            GetSubLayer(pCompositeLayer.get_Layer(i), ref allMapLayer);
        //        }
        //    }
        //    else
        //    {
        //        allMapLayer.Add(pLayer);
        //    }
        //}

        //public void AddLayersToMapControl(ILayer[] layers)
        //{

        //    List<ILayer> annoLayers = null;
        //    List<ILayer> pointLayers = null;
        //    List<ILayer> polylineLayers = null;
        //    List<ILayer> otherLayers = null;
        //    List<ILayer> polygonLayers = null;

        //    #region 按照点线面分别排序
        //    foreach (ILayer layer in layers)
        //    {
        //        if (layer is IFDOGraphicsLayer)
        //        {
        //            if (annoLayers == null)
        //                annoLayers = new List<ILayer>();
        //            annoLayers.Add(layer);
        //        }
        //        else if (layer is IFeatureLayer)
        //        {
        //            IFeatureLayer fLayer = layer as IFeatureLayer;
        //            if (fLayer.FeatureClass != null)
        //            {
        //                #region esriGeometryType
        //                switch (fLayer.FeatureClass.ShapeType)
        //                {
        //                    case esriGeometryType.esriGeometryPoint:
        //                        if (pointLayers == null)
        //                        {
        //                            pointLayers = new List<ILayer>();
        //                        }
        //                        pointLayers.Add(layer);
        //                        break;
        //                    case esriGeometryType.esriGeometryPolyline:
        //                        if (polylineLayers == null)
        //                        {
        //                            polylineLayers = new List<ILayer>();
        //                        }
        //                        polylineLayers.Add(layer);
        //                        break;
        //                    case esriGeometryType.esriGeometryPolygon:
        //                        if (polygonLayers == null)
        //                        {
        //                            polygonLayers = new List<ILayer>();
        //                        }
        //                        polygonLayers.Add(layer);
        //                        break;
        //                    default:
        //                        if (pointLayers == null)
        //                        {
        //                            pointLayers = new List<ILayer>();
        //                        }
        //                        pointLayers.Add(layer);
        //                        break;
        //                }
        //                #endregion
        //            }
        //            else
        //            {
        //                if (otherLayers == null)
        //                {
        //                    otherLayers = new List<ILayer>();
        //                }
        //                otherLayers.Add(layer);
        //            }
        //        }
        //        else
        //        {
        //            if (otherLayers == null)
        //            {
        //                otherLayers = new List<ILayer>();
        //            }
        //            otherLayers.Add(layer);
        //        }
        //    }
        //    #endregion

        //    List<ILayer> allLayers = new List<ILayer>();
        //    if (polygonLayers != null)
        //        allLayers.AddRange(polygonLayers);
        //    if (otherLayers != null)
        //        allLayers.AddRange(otherLayers);
        //    if (polylineLayers != null)
        //        allLayers.AddRange(polylineLayers);
        //    if (pointLayers != null)
        //        allLayers.AddRange(pointLayers);
        //    if (annoLayers != null)
        //        allLayers.AddRange(annoLayers);

        //    //按照点线面的顺序加入到地图上                                           

        //    foreach (ILayer layer in allLayers)
        //    {
        //        if (this.m_MapControl != null)
        //        {
        //            if (layer is IGeoDataset)
        //            {
        //                IGeoDataset geodataset = layer as IGeoDataset;
        //                if (geodataset.SpatialReference != null && (this.m_MapControl.SpatialReference == null || this.m_MapControl.SpatialReference.Name.ToLower() == "unknown"))
        //                {
        //                    this.m_MapControl.SpatialReference = geodataset.SpatialReference;
        //                }
        //            }
        //        }
        //        if (this.m_MapControl != null)
        //            this.m_MapControl.AddLayer(layer, 0);
        //    }
        //    if (this.m_TOCControl != null)
        //        this.m_TOCControl.Update();
        //}
        #endregion

        //public IExtension FindExtensionByCLSID(UID extensionCLSID)
        //{
        //    Guid m_Guid = new Guid(extensionCLSID.Value.ToString());
        //    return Activator.CreateInstance(Type.GetTypeFromCLSID(m_Guid)) as IExtension;
        //}

        //public IExtension FindExtensionByName(string extensionName)
        //{
        //    UID m_Uid = new UIDClass();
        //    m_Uid.Value = extensionName;
        //    Guid m_Guid = new Guid(m_Uid.Value.ToString());
        //    return Activator.CreateInstance(Type.GetTypeFromCLSID(m_Guid)) as IExtension;
        //}
    }
}

#region 注释掉的代码
#region Extension
        //public IExtension FindExtension(object nameOrID)
        //{
        //    return null;
        //}

        //public IExtension get_Extension(int index)
        //{
        //    return null;
 //t = Type.GetTypeFromProgID("esriControls.ControlsEditingVertexDeleteCommand.1");
 //                       o = Activator.CreateInstance(t);
 //                       ICommand deleteVertexCommand = o as ICommand;
        //}

        //public UID get_ExtensionCLSID(int index)
        //{
        //    return null;
        //}
        #endregion
//private object m_MainHook;
//private object m_SecondaryHook;
//private Control m_ActiveMapControl;
//private IAppInfo m_AppInfo;
//private Form m_Form;
//if (this.m_Hook != null)
//{
//    if (this.m_Hook is IMapControl3)
//    {
//        (this.m_Hook as IMapControl3).CurrentTool = null;
//    }
//    else if (this.m_Hook is IPageLayoutControl)
//    {
//        (this.m_Hook as IPageLayoutControl).CurrentTool = null;
//    }
//}
//public void ActiveControl(object hook)
//{
//    if (hook is IMapControl3)
//    {
//        this.m_MapControl = (IMapControl3)hook;
//        if (this.m_PageLayoutControl != null)
//        {
//            m_PageLayoutControl.ActiveView.Deactivate();
//            m_MapControl.ActiveView.Activate(m_MapControl.hWnd);
//        }
//    }
//    else if (hook is IPageLayoutControl2)
//    {
//        IMaps maps = new Maps();
//        maps.Add(this.m_MapControl.Map);
//        m_MapControl.ActiveView.Deactivate();
//        this.m_PageLayoutControl.PageLayout.ReplaceMaps(maps);
//        m_PageLayoutControl.ActiveView.Activate(m_PageLayoutControl.hWnd);
//    }
//    else if (hook is ISceneControl)
//    {
//        ICamera pCamera = m_SceneControl.Camera;
//        pCamera.Azimuth = 180;
//        pCamera.Inclination = 35;
//        pCamera.ViewFieldAngle = 55;
//        if (m_MapControl != null)
//        {
//            IPoint pPtTar = new PointClass();
//            pPtTar.X = (m_MapControl.ActiveView.Extent.XMax + m_MapControl.ActiveView.Extent.XMin) / 2;
//            pPtTar.Y = (m_MapControl.ActiveView.Extent.YMax + m_MapControl.ActiveView.Extent.YMin) / 2;
//            pPtTar.Z = 0;

//            IPoint pPtObs = new PointClass();
//            pPtObs.X = pPtTar.X;
//            pPtObs.Y = pPtTar.Y - this.m_MapControl.ActiveView.Extent.Width;
//            pPtObs.Z = m_MapControl.ActiveView.Extent.Width;

//            pCamera.Target = pPtTar;
//            pCamera.Observer = pPtObs;
//        }
//        m_SceneControl.SceneGraph.RefreshViewers();
//    }
//    this.Hook = hook;
//    this.ActiveView.Refresh();
//}
        //public void LockCustomization(string Password, ICustomizationFilter custFilter)
        //{

        //}

        //public void UnlockCustomization(string Password)
        //{

        //}

   //public void ActivateSceneControl()
   //     {
   //         ICamera pCamera = m_SceneControl.Camera;
   //         pCamera.Azimuth = 180;
   //         pCamera.Inclination = 35;
   //         pCamera.ViewFieldAngle = 55;
   //         if (m_MapControl != null)
   //         {
   //             IPoint pPtTar = new PointClass();
   //             pPtTar.X = (m_MapControl.ActiveView.Extent.XMax + m_MapControl.ActiveView.Extent.XMin) / 2;
   //             pPtTar.Y = (m_MapControl.ActiveView.Extent.YMax + m_MapControl.ActiveView.Extent.YMin) / 2;
   //             pPtTar.Z = 0;

   //             IPoint pPtObs = new PointClass();
   //             pPtObs.X = pPtTar.X;
   //             pPtObs.Y = pPtTar.Y - this.m_MapControl.ActiveView.Extent.Width;
   //             pPtObs.Z = m_MapControl.ActiveView.Extent.Width;

   //             pCamera.Target = pPtTar;
   //             pCamera.Observer = pPtObs;
   //         }
   //         m_SceneControl.SceneGraph.RefreshViewers();
   //     }

   //     public void ActivateGlobeControl()
   //     {
   //     }
#region ApplicationEvents
//private void ApplicationEvents_OnDockWindowAtFixedLocationEvent(object pWindow, Point pt)
//{
//}

//private void ApplicationEvents_OnDisplayStatusInfoEvent(string sInfo)
//{
//}

//private void Application_OnDelayEvents(bool delay)
//{
//    this.DelayEvents(delay);
//}

//private void ApplicationEvents_OnDockWindowAndRefreshEvent(object pWindow)
//{
//}

//private void ApplicationEvents_OnHideDockWindowEvent(object pWindow)
//{
//}

//private void ApplicationEvents_OnCurrentToolChangedEvent(ITool pTool)
//{
//}

//private void ApplicationEvents_OnDockWindowEvent(object pWindow)
//{
//}

//private void ApplicationEvents_OnUpdateContentEvent()
//{
//    this.UpdateContent();
//}
#endregion
//public Control ActiveMapControl
//{
//    get
//    {
//        return this.m_ActiveMapControl;
//    }
//    set
//    {
//        this.m_ActiveMapControl = value;
//    }
//}

//public IAppInfo AppInfo
//{
//    get
//    {
//        return this.m_AppInfo;
//    }
//    set
//    {
//        this.m_AppInfo = value;
//    }
//}
//public object MainHook
//{
//    get
//    {
//        return m_MainHook;
//    }
//    set
//    {
//        m_MainHook = value;
//    }
//}

//public object SecondaryHook
//{
//    get
//    {
//        return this.m_SecondaryHook;
//    }
//    set
//    {
//        this.m_SecondaryHook = value;
//    }
//}

//public IAppInfo AppInfo
//{
//    get
//    {
//    }
//    set
//    {
//        this.pAppInfo = value;
//    }
//}

//public ICommandItem CurrentTool
//{
//    get
//    {
//        return m_CurrentTool;
//    }
//    set
//    {
//        m_CurrentTool = value;
//    }
//}

//public EditorClass Editor
//{
//    get
//    {
//    }
//}

//public EditorExClass EditorEx
//{
//    get
//    {
//    }
//}

//public object SecondaryHook
//{
//    get
//    {
//        return this.secondaryHook;
//    }
//    set
//    {
//        this.secondaryHook = value;
//    }
//}

//public object Hook
//{
//    get
//    {
//        return m_Hook;
//    }
//    set
//    {
//        m_Hook = value;
//    }
//}

//public object MainHook
//{
//    get
//    {
//        return this.mainHook;
//    }
//    set
//    {
//        this.mainHook = value;
//    }
//}

//public IStyleGallery StyleGallery
//{
//    set
//    {
//        this.m_Application.StyleGallery = value;
//    }
//}
//private IPrinter m_Printer;
//private IPaper m_Paper;
//private IAppDisplay m_Display;

//private EditorExClass pEditorExClass = new EditorExClass();
//private EditorClass pEditorClass = new EditorClass();
//private IAppInfo pAppInfo = null;

//private IApplication m_Application = new ApplicationClass();
//private IStyleGallery m_StyleGallery = null;
//public OnClosingWindowEventHandler OnClosingWindowEvent;
//public OnUpdateContentEventHandler OnUpdateContentEvent;
//public OnVersionChangedEventHandler OnVersionChangedEvent;
//public OnDockWindowEventHandler OnDockWindowEvent;
//public OnDelayEventsChangedEventHandler OnDelayEventsChangedEvent;
//public OnDockWindowAtFixedLocationEventHandler OnDockWindowAtFixedLocationEvent;
//public OnDisplayStatusInfoEventHandler OnDisplayStatusInfoEvent;
//public OnHideDockWindowEventHandler OnHideDockWindowEvent;
//public OnLostFocusToCloseEventHandler OnLostFocusToCloseEvent;
//public OnCurrentToolChangedEventHandler OnCurrentToolChangedEvent;
//public OnDockWindowAndRefreshEventHandler OnDockWindowAndRefreshEvent;


//m_MapControl.Map = map;
//if (m_Hook is IMapControl3)
//    this.ActiveView.ContentsChanged();
//else
//{
//    ActiveControl(m_MapControl);
//    this.ActiveView.ContentsChanged();
//    ActiveControl(m_PageLayoutControl);
//}
//m_MapControl.LoadMxFile(path, null, null);
//this.ActiveView.ContentsChanged();
#endregion